package edu.una.moviles.Interfaz;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import java.io.Serializable;

import edu.una.moviles.Modelo.Contenedor;
import edu.una.moviles.Modelo.Desaparecido;
import edu.una.moviles.Modelo.MasBuscado;
import edu.una.moviles.R;

public class SwipeActivity extends ActionBarActivity {


    SectionsPagerAdapter mSectionsPagerAdapter;
    Contenedor contenedor;
    boolean esMasBuscado;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        contenedor = (Contenedor)getIntent().getSerializableExtra("Lista");
        int pos = (int)getIntent().getIntExtra("Pos",0);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setCurrentItem(pos);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.detail_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id ==  R.id.home){
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        if(id==R.id.se_busca){
            Intent lIntent = new Intent(Intent.ACTION_CALL);
            lIntent.setData(Uri.parse("tel:88644241"));
            startActivity(lIntent);
        }
        if(id==R.id.pista){
            Intent lIntent = new Intent(this,BrindarPistaActivity.class);

        lIntent.putExtra("Objeto",(Serializable)contenedor.getLista().get(mViewPager.getCurrentItem()));


            startActivity(lIntent);
        }

        return super.onOptionsItemSelected(item);
    }

    

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            if(contenedor.getLista().get(position) instanceof MasBuscado){
                esMasBuscado = true;
                return MasBuscadoFragment.newInstance(position + 1, (MasBuscado)contenedor.getLista().get(position));
            }
            else
            {
                esMasBuscado = false;
                return DesaparecidoFragment.newInstance(position + 1, (Desaparecido) contenedor.getLista().get(position));
            }
        }

        @Override
        public int getCount() {
            return contenedor.getLista().size();
        }

    }
}
